import storage from './storage'

const ACCOUNT = 'account'
const TOKEN = 'token'

export default {
  name: 'AuthStorage',

  /**
   * 获取 auth，返回：账户信息和 token
   * @return {Object}
   */
  get () {
    return {
      [ACCOUNT]: storage.get(ACCOUNT),
      [TOKEN]: storage.get(TOKEN)
    }
  },

  /**
   * 登录
   * @param {object} account 登录账户
   * @param {string} token 登录 token
   */
  login ({account, token}) {
    storage.set(ACCOUNT, account)
    storage.set(TOKEN, token)
  },

  /**
   * 登出
   */
  logout () {
    storage.remove(ACCOUNT)
    storage.remove(TOKEN)
  },

  /**
   * 是否已登录
   * @return {boolean}
   */
  loggedIn () {
    return !!storage.get(ACCOUNT) && !!storage.get(TOKEN)
  },

  getUser () {
    return storage.get(ACCOUNT)
  },

  getToken () {
    return storage.get(TOKEN)
  }

}
